package cn.wp.ewallet.upms.web.interceptor;

import cn.wp.common.core.WpVar;
import cn.wp.common.core.dto.WpResult;
import cn.wp.common.core.util.ApplicationContextUtil;
import cn.wp.common.core.util.ResponseUtil;
import cn.wp.common.core.web.annotation.BindInterceptor;
import cn.wp.common.core.web.interceptor.LoginInterceptor;
import cn.wp.common.core.web.interceptor.WpAbstractInterceptor;
import cn.wp.ewallet.upms.service.LoginLogService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 重复登录检查
 * Created by chenyb on 2019/10/21
 */
@BindInterceptor(LoginInterceptor.class)
public class RepeatLoginInterceptor extends WpAbstractInterceptor {

    public RepeatLoginInterceptor(String[] pathPatterns, String[] ignores) {
        super(pathPatterns, ignores);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        LoginLogService loginLogService = ApplicationContextUtil.getBean("loginLogServiceImpl");
        String username = request.getHeader("username");
        String uuid = request.getHeader("uuid");
        boolean check = loginLogService.checkLoginEquipment(username,uuid);
        if(!check){
            WpResult wpResult = new WpResult().failure(WpVar.REPEAT_LOGIN_CODE,"重复登录");
            ResponseUtil.writeJson(response,wpResult);
        }
        return check;
    }

}
